package com.yjy;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
public class StartInit implements SmartInitializingSingleton {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void afterSingletonsInstantiated() {
        // 连续发送1000条记录，tag1有500条，tag2有500条
        int index = 1;
        String tag1 = "tag1";
        String tag2 = "tag2";
//        for (int i = 0, len = 1000; i < len; i++) {
//            String tag = (index % 2 == 0) ? tag1 : tag2;
//            rocketMQTemplate.send("test-v10-topic" + ":" + tag, MessageBuilder.withPayload(index++).build());
//        }

        for (int i = 0; i < 500; i++) {
            rocketMQTemplate.send("test-v10-topic" + ":" + tag1, MessageBuilder.withPayload(index++).build());
        }
        for (int i = 0; i < 500; i++) {
            rocketMQTemplate.send("test-v10-topic" + ":" + tag2, MessageBuilder.withPayload(index++).build());
        }
    }
}
